<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//-------------------------------------
// 文件说明：生成验证码
//-------------------------------------
session_start();
class Image{
function get() {
  //不存在imageCreate函数则认为当前环境不支持GD库
  if (function_exists('imagecreate')) {
   //产生4个字符的随机字符串作为验证码
   $str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
   $code = array();
   for ($i=0; $i<4; $i++) {
    $code[] = $str[mt_rand(1,strlen($str))];
   }
   //将验证码写入到Session，忽略大小写
   $vdcode = strtolower(implode('',$code));
   $_SESSION['vdcode']=$vdcode;
   $width = 50; //图片宽度
   $height = 20; //图片高度
   $im = ImageCreate($width,$height); //创建图形
   ImageColorAllocate($im,255,255,255); //填充背景颜色为白色
   //用淡色给图形添加杂色
   for ($i=0; $i<100; $i++) {
    $pxcolor = ImageColorAllocate($im,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
    ImageSetPixel($im,mt_rand(0,$width),mt_rand(0,$height),$pxcolor);
   }
   //用比较明显的颜色写上验证码文字
   $offset = 5;
   foreach ($code as $char) {
    $textcolor = ImageColorAllocate($im,mt_rand(0,250),mt_rand(0,150),mt_rand(0,250));
    ImageChar($im,5,$offset,2,$char,$textcolor);
    $offset += 10;
   }
   //禁止缓存
   header("pragma:no-cachern");
   header("Cache-Control:no-cachern");
   header("Expires:0rn");
   //检查系统支持的文件类型，优先级为PNG->JPEG->GIF
   if (ImageTypes() & IMG_PNG) {
    header('Content-Type:image/png');
    ImagePNG($im);
   } elseif (ImageTypes() & IMG_JPEG) {
    header('Content-Type:image/jpeg');
    ImageJPEG($im);
   } else {
    header('Content-Type:image/gif');
    ImageGif($im);
   }
  } else {
   //不支持GD库，则输出默认验证码ABCD
   $_SESSION['vdcode']='abcd';
   header('Content-Type:image/jpeg');
   $fp = fopen('vdcode.jpg','rb');
   return fread($fp,filesize('vdcode.jpg'));
   fclose($fp);
  }
}
}
?>
